#ITEM MODELS
from django.db import models
from game.inventory.models import itemInstance
from game.polymorphic import PolymorphicModel

class itemType(models.Model):
	name = models.CharField(max_length=20, unique=True)
	slotUsed = models.ForeignKey('inventory.itemSlot', blank=True, null=True)
	equipable = models.BooleanField(default=1)
	def __unicode__(self):
		return self.name
			
class material(models.Model):
	name = models.CharField(max_length=20, unique=True)
	damageModifierCollection = models.ForeignKey('damage.damageModifierCollection', null=True) 
	
	def __unicode__(self):
		return self.name
		
	def getDefence(self, damageType):
		""" returns defence value for material against a damageType """
		if self.damageModifierCollection is None:  #not all materials resist
			return 0
		else:
			return self.damageModifierCollection.getDefence(damageType)
			
	def getAttack(self, damageType):
		"""returns attack bonus for a material for a damageType """
		if self.damageModifierCollection is None:
			return 0
		else:
			return self.damageModifierCollection.getAttack(damageType)
	
class item(PolymorphicModel):
	name =	models.CharField(max_length=20, unique=True)
	itemType = models.ForeignKey('itemType', default=1)
	
	def __unicode__(self):
		return self.name

class armor(item):
	damageModifierCollection = models.ForeignKey('damage.damageModifierCollection')
	maxDex = models.IntegerField(default=50)
	
	def getDefence(self, dt):
		"""
		Returns the armors defence against a given damageType.
		"""
		power = 0
		power += self.damageModifierCollection.getDefence(dt)
		return power
	
class weapon(item, models.Model):
	attack = models.ForeignKey('actions.attack')
	
	def getDamage(self):
		damage = self.attack.damage
		return damage
	
	@property
	def damage(self):
		return self.getDamage()
